// JavaScript Document
$(document).ready(function(){
	/*列表控件*/
	$('#sendemail_group_tt').datagrid({
		title: '邮件群发队列'
		, fit: true
		, url: 'sendemail-group.php?method=GetData'
		, pagination: true /*底部分页栏*/
		, singleSelect: true
		, pageList: [100, 200, 500]
		, loadMsg: '正在加载数据，请稍等 ...'
		, columns:[[
				{field:'id', title:'ID序号', width:40}
				, {field:'to', title:'群发对象', width:100, align:'center'}
				, {field:'from', title:'群发邮箱', width:150, align:'center', formatter: function(value, rowData){
					return "<a href='javascript:void(0)' onclick='ShowFrom(\"" + value + "\")'>" + value + "</a>";
				}}
				, {field:'subject', title:'标题', width:150, align:'center', formatter: function(value, rowData){
					return "<a href='javascript:void(0)' onclick='ShowBody(" + rowData.id + ")'>" + value + "</a>";
				}}
				, {field:'run', title:'执行状态', width:60, align:'center', formatter: function(value, rowData){
					if(rowData.flag > 0 && rowData.flag < 32){
						if(0 != value)
							return "<font color=green>正在执行</font>";
						else 
							return "<font color=red>暂停执行</font>";
					} else {
						return "<font color=gray>停止执行</font>";
					}
				}}
				, {field:'flag', title:'邮件标识', width:60, align:'center'}
				, {field:'max', title:'单次最大发送数', width:90, align:'center'}
				, {field:'val', title:'时间间隔(分)', width:80, align:'center'}
				, {field:'sucess', title:'成功数', width:60, align:'center'}
				, {field:'failure', title:'失败数', width:60, align:'center', formatter: function(value, rowData){
					if(0 == value)
						return value;
					return "<font color=red>" + value + "</font>";
				}}
				, {field:'firstdt', title:'第一次执行时间', width:100, align:'center'}
				, {field:'lastdt', title:'最后一次执行时间', width:100, align:'center'}
				, {field:'#opt', title:'操作', width:120, align:'center', formatter: function(value, rowData){
					var str = "<a href='javascript:void(0)' onclick='DeleteEmail(" + rowData.id + ")'>删除</a>&nbsp;";
					if(0 != rowData.run || 0 >= rowData.flag || 32 <= rowData.flag)
						str += "<font color=gray>执行</font>";
					else
						str += "<a href='javascript:void(0)' onclick='StartEmail(" + rowData.id + ")'>执行</a>";
					str += "&nbsp;";
					if(0 != rowData.run && 0 < rowData.flag && 32 > rowData.flag)
						str += "<a href='javascript:void(0)' onclick='PauseEmail(" + rowData.id + ")'>暂停</a>";
					else
						str += "<font color=gray>暂停</font>";
					str += "&nbsp;";
					if(0 < rowData.flag && 32 > rowData.flag)
						str += "<a href='javascript:void(0)' onclick='RemoveEmail(" + rowData.id + ")'>停止</a>";
					else
						str += "<font color=gray>停止</font>";
					return str;					
				}}
			]]
		, rowStyler:function(index,rowData){
        	if (rowData.flag <= 0 || rowData.flag >= 32){
            	return 'background-color:#dddddd';
        	}
    	}
  	});
});

/*显示收件人信息*/
function ShowFrom(email){
}

/*显示邮件内容*/
function ShowBody(id){
}

/*删除操作*/
function DeleteEmail(id){
	top.$.messager.confirm('确认操作', '确实要删除id为' + id + '的发送队列吗？'
		, function(b){
			if(!b) return;
			$.ajax({type:'POST'
				, url: 'sendemail-group-action.php'
				, data: 'type=Delete&id=' + id
				, dataType: 'json'
				, error: function(XMLHttpRequest, textStatus, errorThrown){
					top.$.messager.alert('错误', '请求失败:' + errorThrown, 'error');
				}
				,success: function(msg){
					if(msg.success){
						top.$.messager.alert('操作成功', msg.msg, 'info');
						$('#sendemail_group_tt').datagrid('load');
					} else {
						top.$.messager.alert('操作失败', msg.msg, 'error');
					}
				}
			});
		}
	);
}

/*启动操作*/
function StartEmail(id){
	$.ajax({type:'POST'
		, url: 'sendemail-group-action.php'
		, data: 'type=Start&id=' + id
		, dataType: 'json'
		, error: function(XMLHttpRequest, textStatus, errorThrown){
			top.$.messager.alert('错误', '请求失败:' + errorThrown, 'error');
		}
		,success: function(msg){
			if(msg.success){
				$('#sendemail_group_tt').datagrid('reload');
			} else {
				top.$.messager.alert('操作失败', msg.msg, 'error');
			}
		}
	});
}

/*暂停操作*/
function PauseEmail(id){
	$.ajax({type:'POST'
		, url: 'sendemail-group-action.php'
		, data: 'type=Pause&id=' + id
		, dataType: 'json'
		, error: function(XMLHttpRequest, textStatus, errorThrown){
			top.$.messager.alert('错误', '请求失败:' + errorThrown, 'error');
		}
		,success: function(msg){
			if(msg.success){
				$('#sendemail_group_tt').datagrid('reload');
			} else {
				top.$.messager.alert('操作失败', msg.msg, 'error');
			}
		}
	});
}

/*停止操作*/
function RemoveEmail(id){
	top.$.messager.confirm('确认操作', '停止发送队列以后将不可恢复。<br>确实要停止id为' + id + '的发送队列吗？'
		, function(b){
			if(!b) return;
			$.ajax({type:'POST'
				, url: 'sendemail-group-action.php'
				, data: 'type=Stop&id=' + id
				, dataType: 'json'
				, error: function(XMLHttpRequest, textStatus, errorThrown){
					top.$.messager.alert('错误', '请求失败:' + errorThrown, 'error');
				}
				,success: function(msg){
					if(msg.success){
						top.$.messager.alert('操作成功', msg.msg, 'info');
						$('#sendemail_group_tt').datagrid('reload');
					} else {
						top.$.messager.alert('操作失败', msg.msg, 'error');
					}
				}
			});
		}
	);
}